package com.start.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.CompanyDao;
import com.start.manager.domain.entity.Company;
import com.start.manager.domain.vo.CompanyVO;
import com.start.manager.domain.vo.OptionsVO;
import com.start.manager.service.ICompanyService;
import com.start.manager.util.DateUtil;
import com.start.manager.util.MyUtil;
import com.start.manager.util.PageUtil;
import com.start.manager.util.constant.RedisConstant;
import com.jienlee.common.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, Company> implements ICompanyService {

    @Autowired
    private MyUtil myUtil;

    @Override
    public PageUtil findList(CompanyVO companyVO) {
        LambdaQueryWrapper<Company> queryWrapper = initQueryWrapper(companyVO);
        queryWrapper.orderByDesc(Company::getId);
        if (companyVO.getSize() == NumberUtils.INTEGER_ZERO) {
            //获取全部的数据
            List<Company> list = this.baseMapper.selectList(queryWrapper);
            return new PageUtil(list.size(), companyVO.getPage(), list);
        }
        //分页查询货物详情
        Page<Company> page = new Page<>(companyVO.getPage(), companyVO.getSize());
        Page<Company> pageData = this.baseMapper.selectPage(page, queryWrapper);
        return new PageUtil(pageData.getTotal(), pageData.getCurrent(), pageData.getRecords());

    }

    private LambdaQueryWrapper<Company> initQueryWrapper(CompanyVO companyVO) {
        LambdaQueryWrapper<Company> queryWrapper = new QueryWrapper<Company>().lambda();
        if (StringUtils.isNotBlank(companyVO.getName())) {
            queryWrapper.like(Company::getName, companyVO.getName());
        }
        if (StringUtils.isNotBlank(companyVO.getType())) {
            queryWrapper.eq(Company::getType, companyVO.getType());
        }
        if (StringUtils.isNotBlank(companyVO.getId())) {
            queryWrapper.eq(Company::getId, companyVO.getId());
        }
        return queryWrapper;
    }

    @Override
    public boolean saveCompany(CompanyVO companyVO) {
        Company company = new Company();
        String dataTime = DateUtil.getNowTime_EN();
        if (StringUtils.isBlank(companyVO.getId())) {
            company.setCreateTime(dataTime);
            company.setUpdateTime(dataTime);
        } else {
            company.setUpdateTime(dataTime);
        }
        BeanUtils.copyProperties(companyVO, company);
        boolean save = saveOrUpdate(company);
        return save;
    }

    @Override
    public List<Company> getOptionsList(OptionsVO optionsVO) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", optionsVO.getType());
        queryWrapper.orderByAsc("id");
        return this.baseMapper.selectList(queryWrapper);
    }

}
